/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SuratKeteranganDialog.java
 *
 * Created on Nov 7, 2011, 2:36:22 PM
 */
package com.x3.dishub.ui;

import com.x3.dishub.dao.KendaraanDAO;
import com.x3.dishub.dao.SuratKeteranganDAO;
import com.x3.dishub.dao.WarnaDAO;
import com.x3.dishub.entity.Kendaraan;
import com.x3.dishub.entity.SuratKeterangan;
import com.x3.dishub.entity.Warna;
import java.awt.Color;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import javax.swing.JColorChooser;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer;

/**
 *
 * @author Hendro Steven
 */
public class SuratKeteranganDialog extends javax.swing.JDialog {

    private WarnaDAO wDao = (WarnaDAO) MainApps.appContext.getBean("warnaDAO");
    private Warna headerColor;
    private Warna backgroudColor;
    private SuratKeteranganDAO dao = (SuratKeteranganDAO) MainApps.appContext.getBean("suratKeteranganDAO");
    private List<SuratKeterangan> list;
    private SuratKeterangan selected;
    private SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
    private KendaraanDAO kDao = (KendaraanDAO) MainApps.appContext.getBean("kendaraanDAO");
    private Kendaraan selectedKendaraan;
    private List<Kendaraan> listKendaraan;

    /** Creates new form SuratKeteranganDialog */
    public SuratKeteranganDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        //this.getContentPane().setBackground(new java.awt.Color(51, 102, 255));
        loadWarna();
        updateWarna();
        loadSurat();
    }
    
    private void loadWarna() {
        headerColor = wDao.getByNama("SuratKeteranganDialogHd");
        if (headerColor == null) {
            headerColor = new Warna();
            headerColor.setName("SuratKeteranganDialogHd");
            headerColor.setRgb(-16777012);
            wDao.insert(headerColor);
            loadWarna();
        }
        backgroudColor = wDao.getByNama("SuratKeteranganDialogBg");
        if (backgroudColor == null) {
            backgroudColor = new Warna();
            backgroudColor.setName("SuratKeteranganDialogBg");
            backgroudColor.setRgb(-13408513);
            wDao.insert(backgroudColor);
            loadWarna();
        }
    }

    private void updateWarna() {
        headerPanel.setBackground(new Color(headerColor.getRgb()));
        this.getContentPane().setBackground(new Color(backgroudColor.getRgb()));
    }

    private void loadSurat() {
        try {
            list = dao.getAllSuratKeterangan();
            String title[] = {"Nomor", "Tanggal", "No.Kendaraan", "Pemilik Baru"};
            Object data[][] = new Object[list.size()][4];
            int row = 0;
            for (SuratKeterangan s : list) {
                data[row][0] = s.getNomor();
                data[row][1] = formater.format(s.getTanggal());
                data[row][2] = s.getKendaraan().getNomorPolisi();
                data[row][3] = s.getKendaraan().getPemilik().get(s.getKendaraan().getPemilik().size() - 1).getNamaDepan() + " "
                        + s.getKendaraan().getPemilik().get(s.getKendaraan().getPemilik().size() - 1).getNamaBelakang();
                ++row;
            }
            TableModel model = new DefaultTableModel(data, title);
            tblSurat.setModel(model);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        browseKendaraan = new javax.swing.JDialog();
        jLabel22 = new javax.swing.JLabel();
        txtCariNomorPolisi = new javax.swing.JTextField();
        btnCariKendaraan1 = new javax.swing.JButton();
        jScrollPane6 = new javax.swing.JScrollPane();
        tblBrowseKendaraan = new javax.swing.JTable();
        headerPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSurat = new javax.swing.JTable();
        btnCariNomor = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnSimpan = new javax.swing.JButton();
        btnBuatBaru = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        btnCetak = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txtMerkTypeJenis = new javax.swing.JTextField();
        txtNomor = new javax.swing.JTextField();
        tglSurat = new org.jdesktop.swingx.JXDatePicker();
        txtKendaraan = new javax.swing.JTextField();
        btnBrowseKendaraan = new javax.swing.JButton();
        txtNoUji = new javax.swing.JTextField();
        txtNoRangka = new javax.swing.JTextField();
        txtNoMesin = new javax.swing.JTextField();
        txtTahun = new javax.swing.JTextField();
        txtPemilik = new javax.swing.JTextField();
        txtAlamatPemilik = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtLainLain = new javax.swing.JTextArea();
        jLabel14 = new javax.swing.JLabel();
        txtPemilikBaru = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        txtAlamatPemilikBaru = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnuWarna = new javax.swing.JMenu();
        mnuHeader = new javax.swing.JMenuItem();
        mnuBack = new javax.swing.JMenuItem();
        mnuDefault = new javax.swing.JMenuItem();

        jLabel22.setText("Nomor Polisi :");

        btnCariKendaraan1.setText("Cari");
        btnCariKendaraan1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariKendaraan1ActionPerformed(evt);
            }
        });

        tblBrowseKendaraan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblBrowseKendaraan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblBrowseKendaraanMouseClicked(evt);
            }
        });
        jScrollPane6.setViewportView(tblBrowseKendaraan);

        javax.swing.GroupLayout browseKendaraanLayout = new javax.swing.GroupLayout(browseKendaraan.getContentPane());
        browseKendaraan.getContentPane().setLayout(browseKendaraanLayout);
        browseKendaraanLayout.setHorizontalGroup(
            browseKendaraanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(browseKendaraanLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(browseKendaraanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE)
                    .addGroup(browseKendaraanLayout.createSequentialGroup()
                        .addComponent(jLabel22)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCariNomorPolisi, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCariKendaraan1)))
                .addContainerGap())
        );
        browseKendaraanLayout.setVerticalGroup(
            browseKendaraanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(browseKendaraanLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(browseKendaraanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(txtCariNomorPolisi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCariKendaraan1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Surat Keterangan");

        headerPanel.setBackground(new java.awt.Color(0, 0, 204));
        headerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 20));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Surat Keterangan");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Pembuatan Surat Keterangan");

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2))
                    .addComponent(jLabel1))
                .addContainerGap(604, Short.MAX_VALUE))
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblSurat.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblSurat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSuratMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblSurat);

        btnCariNomor.setText("Cari Berdasarkan Nomor");
        btnCariNomor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariNomorActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh Tabel");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 752, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnCariNomor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRefresh)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCariNomor)
                    .addComponent(btnRefresh))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Daftar Surat Keterangan", jPanel2);

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        btnBuatBaru.setText("Buat Baru");
        btnBuatBaru.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuatBaruActionPerformed(evt);
            }
        });

        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });

        btnCetak.setText("Cetak Surat Keterangan");
        btnCetak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCetakActionPerformed(evt);
            }
        });

        jLabel3.setText("Nomor :");

        jLabel4.setText("Tanggal :");

        jLabel5.setText("Kendaraan :");

        jLabel6.setText("Nomor Uji Kendaraan :");

        jLabel7.setText("Merk/Type/Jenis Kendr :");

        jLabel8.setText("Nomor Chassis/Rangka :");

        jLabel9.setText("Nomor Mesin :");

        jLabel10.setText("Tahun Kendaraan :");

        jLabel11.setText("Nama Pemilik Lama :");

        jLabel12.setText("Alamat Pemilik Lama :");

        jLabel13.setText("Lain - lain :");

        txtMerkTypeJenis.setEditable(false);

        txtKendaraan.setEditable(false);

        btnBrowseKendaraan.setText("....");
        btnBrowseKendaraan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseKendaraanActionPerformed(evt);
            }
        });

        txtNoUji.setEditable(false);

        txtNoRangka.setEditable(false);

        txtNoMesin.setEditable(false);

        txtTahun.setEditable(false);

        txtPemilik.setEditable(false);

        txtAlamatPemilik.setEditable(false);

        txtLainLain.setColumns(20);
        txtLainLain.setRows(5);
        jScrollPane2.setViewportView(txtLainLain);

        jLabel14.setText("Nama Pemilik Baru :");

        txtPemilikBaru.setEditable(false);

        jLabel15.setText("Alamat Pemilik Baru :");

        txtAlamatPemilikBaru.setEditable(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMerkTypeJenis, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtNoUji, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                            .addComponent(tglSurat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtNomor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                            .addComponent(txtKendaraan, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBrowseKendaraan))
                    .addComponent(txtAlamatPemilik, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtTahun, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtNoMesin)
                        .addComponent(txtNoRangka, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                        .addComponent(txtPemilik))
                    .addComponent(txtAlamatPemilikBaru, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                    .addComponent(txtPemilikBaru, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(btnCetak)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 382, Short.MAX_VALUE)
                .addComponent(btnHapus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBuatBaru)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSimpan)
                .addGap(10, 10, 10))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNomor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tglSurat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtKendaraan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBrowseKendaraan))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtNoUji, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtMerkTypeJenis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtNoRangka, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtNoMesin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtTahun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtPemilik, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtAlamatPemilik, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(txtPemilikBaru, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(txtAlamatPemilikBaru, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(59, 59, 59)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSimpan)
                    .addComponent(btnBuatBaru)
                    .addComponent(btnHapus)
                    .addComponent(btnCetak))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Detail Surat Keterangan", jPanel3);

        mnuWarna.setText("Seting");

        mnuHeader.setText("Warna Header");
        mnuHeader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuHeaderActionPerformed(evt);
            }
        });
        mnuWarna.add(mnuHeader);

        mnuBack.setText("Warna Background");
        mnuBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuBackActionPerformed(evt);
            }
        });
        mnuWarna.add(mnuBack);

        mnuDefault.setText("Warna Default");
        mnuDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuDefaultActionPerformed(evt);
            }
        });
        mnuWarna.add(mnuDefault);

        jMenuBar1.add(mnuWarna);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 777, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCariNomorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariNomorActionPerformed
        try {
            String nomor = JOptionPane.showInputDialog(this, "Nomor :");
            list = dao.getByNomor(nomor);
            String title[] = {"Nomor", "Tanggal", "No.Kendaraan", "Pemilik Baru"};
            Object data[][] = new Object[list.size()][4];
            int row = 0;
            for (SuratKeterangan s : list) {
                data[row][0] = s.getNomor();
                data[row][1] = formater.format(s.getTanggal());
                data[row][2] = s.getKendaraan().getNomorPolisi();
                data[row][3] = s.getKendaraan().getPemilik().get(s.getKendaraan().getPemilik().size() - 1).getNamaDepan() + " "
                        + s.getKendaraan().getPemilik().get(s.getKendaraan().getPemilik().size() - 1).getNamaBelakang();
                ++row;
            }
            TableModel model = new DefaultTableModel(data, title);
            tblSurat.setModel(model);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnCariNomorActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        // TODO add your handling code here:
        loadSurat();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void tblSuratMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSuratMouseClicked
        selected = (SuratKeterangan) list.get(tblSurat.getSelectedRow());
        txtNomor.setText(selected.getNomor());
        tglSurat.setDate(selected.getTanggal());
        txtKendaraan.setText(selected.getKendaraan().getNomorPolisi());
        txtNoUji.setText(selected.getKendaraan().getNomorUji());
        txtMerkTypeJenis.setText(selected.getKendaraan().getMerk().getNama()
                + "/" + selected.getKendaraan().getType() + "/" + selected.getKendaraan().getJenis().getNama());
        txtNoRangka.setText(selected.getKendaraan().getNomorRangka());
        txtNoMesin.setText(selected.getKendaraan().getNomorMesin());
        txtTahun.setText(selected.getKendaraan().getTahunPembuatan());
        txtPemilik.setText(selected.getKendaraan().getPemilik().get(
                selected.getKendaraan().getPemilik().size() - 2).getNamaDepan() + " "
                + selected.getKendaraan().getPemilik().get(
                selected.getKendaraan().getPemilik().size() - 2).getNamaBelakang());
        txtAlamatPemilik.setText(selected.getKendaraan().getPemilik().get(
                selected.getKendaraan().getPemilik().size() - 2).getAlamat());
        txtPemilikBaru.setText(selected.getKendaraan().getPemilik().get(
                selected.getKendaraan().getPemilik().size() - 1).getNamaDepan() + " "
                + selected.getKendaraan().getPemilik().get(
                selected.getKendaraan().getPemilik().size() - 1).getNamaBelakang());
        txtAlamatPemilikBaru.setText(selected.getKendaraan().getPemilik().get(
                selected.getKendaraan().getPemilik().size() - 1).getAlamat());
        txtLainLain.setText(selected.getLainLain());
    }//GEN-LAST:event_tblSuratMouseClicked

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        try {
            if (selected != null) {
                dao.delete(selected);
                selected = null;
                txtNomor.setText("");
                tglSurat.setDate(new Date());
                txtKendaraan.setText("");
                txtNoUji.setText("");
                txtMerkTypeJenis.setText("");
                txtNoRangka.setText("");
                txtNoMesin.setText("");
                txtTahun.setText("");
                txtPemilik.setText("");
                txtAlamatPemilik.setText("");
                txtLainLain.setText("");
                txtPemilikBaru.setText("");
                txtAlamatPemilikBaru.setText("");
                txtLainLain.setText("");
                loadSurat();
                JOptionPane.showMessageDialog(this, "Data terhapus");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnHapusActionPerformed

    private void btnBuatBaruActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuatBaruActionPerformed
        selected = null;
        txtNomor.setText("");
        tglSurat.setDate(new Date());
        txtKendaraan.setText("");
        txtNoUji.setText("");
        txtMerkTypeJenis.setText("");
        txtNoRangka.setText("");
        txtNoMesin.setText("");
        txtTahun.setText("");
        txtPemilik.setText("");
        txtAlamatPemilik.setText("");
        txtPemilikBaru.setText("");
        txtAlamatPemilikBaru.setText("");
        txtLainLain.setText("");
        txtNomor.requestFocus();
    }//GEN-LAST:event_btnBuatBaruActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        try {
            if (selected == null) {
                SuratKeterangan s = new SuratKeterangan();
                s.setNomor(txtNomor.getText());
                s.setTanggal(tglSurat.getDate());
                s.setKendaraan(selectedKendaraan);
                s.setPemilikBaru(selectedKendaraan.getPemilik().get(selectedKendaraan.getPemilik().size() - 1));
                s.setPemilik(selectedKendaraan.getPemilik().get(selectedKendaraan.getPemilik().size() - 2));
                s.setLainLain(txtLainLain.getText());
                dao.insert(s);
                selectedKendaraan = null;
            } else {
                selected.setNomor(txtNomor.getText());
                selected.setTanggal(tglSurat.getDate());
                selected.setKendaraan(selectedKendaraan);
                selected.setPemilikBaru(selectedKendaraan.getPemilik().get(selectedKendaraan.getPemilik().size() - 1));
                selected.setPemilik(selectedKendaraan.getPemilik().get(selectedKendaraan.getPemilik().size() - 2));
                selected.setLainLain(txtLainLain.getText());
                dao.update(selected);
                selected = null;
            }
            loadSurat();
            txtNomor.setText("");
            tglSurat.setDate(new Date());
            txtKendaraan.setText("");
            txtNoUji.setText("");
            txtMerkTypeJenis.setText("");
            txtNoRangka.setText("");
            txtNoMesin.setText("");
            txtTahun.setText("");
            txtPemilik.setText("");
            txtAlamatPemilik.setText("");
            txtPemilikBaru.setText("");
            txtAlamatPemilikBaru.setText("");
            txtLainLain.setText("");
            JOptionPane.showMessageDialog(this, "Data tersimpan");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnSimpanActionPerformed

    private void btnCariKendaraan1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariKendaraan1ActionPerformed
        try {
            listKendaraan = kDao.getByNomorPolisi(txtCariNomorPolisi.getText());
            String title[] = {"No. Polisi", "No.Uji", "Merk/Type", "Tahun"};
            Object[][] data = new Object[listKendaraan.size()][4];
            int row = 0;
            for (Kendaraan k : listKendaraan) {
                data[row][0] = k.getNomorPolisi();
                data[row][1] = k.getNomorUji();
                data[row][2] = k.getMerk() + k.getType();
                data[row][3] = k.getTahunPembuatan();
                ++row;
            }
            TableModel model = new DefaultTableModel(data, title);
            tblBrowseKendaraan.setModel(model);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
}//GEN-LAST:event_btnCariKendaraan1ActionPerformed

    private void tblBrowseKendaraanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblBrowseKendaraanMouseClicked
        try {
            selectedKendaraan = (Kendaraan) listKendaraan.get(tblBrowseKendaraan.getSelectedRow());
            txtKendaraan.setText(selectedKendaraan.getNomorPolisi());
            txtNoUji.setText(selectedKendaraan.getNomorUji());
            txtMerkTypeJenis.setText(selectedKendaraan.getMerk().getNama()
                    + "/" + selectedKendaraan.getType() + "/" + selectedKendaraan.getJenis().getNama());
            txtNoRangka.setText(selectedKendaraan.getNomorRangka());
            txtNoMesin.setText(selectedKendaraan.getNomorMesin());
            txtTahun.setText(selectedKendaraan.getTahunPembuatan());
            txtPemilik.setText(selectedKendaraan.getPemilik().get(
                    selectedKendaraan.getPemilik().size() - 2).getNamaDepan() + " "
                    + selectedKendaraan.getPemilik().get(
                    selectedKendaraan.getPemilik().size() - 2).getNamaBelakang());
            txtAlamatPemilik.setText(selectedKendaraan.getPemilik().get(
                    selectedKendaraan.getPemilik().size() - 2).getAlamat());

            txtPemilikBaru.setText(selectedKendaraan.getPemilik().get(
                    selectedKendaraan.getPemilik().size() - 1).getNamaDepan() + " "
                    + selectedKendaraan.getPemilik().get(
                    selectedKendaraan.getPemilik().size() - 1).getNamaBelakang());
            txtAlamatPemilikBaru.setText(selectedKendaraan.getPemilik().get(
                    selectedKendaraan.getPemilik().size() - 1).getAlamat());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
        browseKendaraan.setVisible(false);
}//GEN-LAST:event_tblBrowseKendaraanMouseClicked

    private void btnBrowseKendaraanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseKendaraanActionPerformed
        try {
            listKendaraan = kDao.getAll();
            String title[] = {"No. Polisi", "No.Uji", "Merk/Type", "Tahun"};
            Object[][] data = new Object[listKendaraan.size()][4];
            int row = 0;
            for (Kendaraan k : listKendaraan) {
                data[row][0] = k.getNomorPolisi();
                data[row][1] = k.getNomorUji();
                data[row][2] = k.getMerk() + k.getType();
                data[row][3] = k.getTahunPembuatan();
                ++row;
            }
            TableModel model = new DefaultTableModel(data, title);
            tblBrowseKendaraan.setModel(model);
            browseKendaraan.setSize(800, 600);
            browseKendaraan.getContentPane().setBackground(new java.awt.Color(51, 102, 255));
            browseKendaraan.setModal(true);
            browseKendaraan.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnBrowseKendaraanActionPerformed

    private void btnCetakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCetakActionPerformed
        try {
            if (selected != null) {
                //ambil parameter
                Map mp = new HashMap();
                mp.put("nomor", selected.getNomor());

                DataSource ds = (DataSource) MainApps.appContext.getBean("dataSource");
                JDialog printView = new JDialog();
                JasperPrint print = JasperFillManager.fillReport("print/suratKeterangan.jasper",
                        mp, ds.getConnection());
                JRViewer viewer = new JRViewer(print);
                printView.getContentPane().add(viewer);
                printView.setModal(false);
                printView.setTitle("Print Surat Keterangan");
                printView.setSize(800, 500);
                printView.setVisible(true);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnCetakActionPerformed

    private void mnuHeaderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuHeaderActionPerformed
        Color tmp = JColorChooser.showDialog(this, "Ganti Warna Header", headerPanel.getBackground());
        headerColor.setRgb(tmp.getRGB());
        wDao.update(headerColor);
        updateWarna();
    }//GEN-LAST:event_mnuHeaderActionPerformed

    private void mnuBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuBackActionPerformed
        Color tmp = JColorChooser.showDialog(this, "Ganti Warna Backgroud", this.getContentPane().getBackground());
        backgroudColor.setRgb(tmp.getRGB());
        wDao.update(backgroudColor);
        updateWarna();
    }//GEN-LAST:event_mnuBackActionPerformed

    private void mnuDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuDefaultActionPerformed
        headerColor.setRgb(-16777012);
        backgroudColor.setRgb(-13408513);
        wDao.update(headerColor);
        wDao.update(backgroudColor);
        updateWarna();
    }//GEN-LAST:event_mnuDefaultActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog browseKendaraan;
    private javax.swing.JButton btnBrowseKendaraan;
    private javax.swing.JButton btnBuatBaru;
    private javax.swing.JButton btnCariKendaraan1;
    private javax.swing.JButton btnCariNomor;
    private javax.swing.JButton btnCetak;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JMenuItem mnuBack;
    private javax.swing.JMenuItem mnuDefault;
    private javax.swing.JMenuItem mnuHeader;
    private javax.swing.JMenu mnuWarna;
    private javax.swing.JTable tblBrowseKendaraan;
    private javax.swing.JTable tblSurat;
    private org.jdesktop.swingx.JXDatePicker tglSurat;
    private javax.swing.JTextField txtAlamatPemilik;
    private javax.swing.JTextField txtAlamatPemilikBaru;
    private javax.swing.JTextField txtCariNomorPolisi;
    private javax.swing.JTextField txtKendaraan;
    private javax.swing.JTextArea txtLainLain;
    private javax.swing.JTextField txtMerkTypeJenis;
    private javax.swing.JTextField txtNoMesin;
    private javax.swing.JTextField txtNoRangka;
    private javax.swing.JTextField txtNoUji;
    private javax.swing.JTextField txtNomor;
    private javax.swing.JTextField txtPemilik;
    private javax.swing.JTextField txtPemilikBaru;
    private javax.swing.JTextField txtTahun;
    // End of variables declaration//GEN-END:variables
}
